package com.bytz.modules.cms.reseller.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytz.common.constant.CommonConstant;
import com.bytz.modules.cms.domain.manager.CmsManager;
import com.bytz.modules.cms.domain.model.price.PriceComputeResult;
import com.bytz.modules.cms.product.entity.Product;
import com.bytz.modules.cms.product.service.IProductService;
import com.bytz.modules.cms.reseller.entity.ResellerProduct;
import com.bytz.modules.cms.reseller.mapper.ResellerProductMapper;
import com.bytz.modules.cms.reseller.model.ResellerProductModel;
import com.bytz.modules.cms.reseller.service.IResellerProductService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 莱宝CMS—经销商管理—经销商/商品关联表 服务实现类
 * </p>
 *
 * @author KyleWang
 * @since 2022-10-10
 */
@Service
public class ResellerProductServiceImpl extends ServiceImpl<ResellerProductMapper, ResellerProduct> implements IResellerProductService {

    @Autowired
    @Lazy
    private CmsManager cmsManager;

    @Autowired
    private IProductService productService;

    @Override
    public List<ResellerProduct> getByResellerId(String id) {

        return lambdaQuery().eq(ResellerProduct::getResellerId, id).list();
    }

    @Override
    public List<ResellerProduct> getByResellerAndProductNumbers(String resellerId, Collection<String> productNumbers) {

        return lambdaQuery()
                .eq(ResellerProduct::getResellerId, resellerId)
                .in(ResellerProduct::getProductNumber, productNumbers)
                .list();
    }

    @Override
    public IPage<ResellerProductModel> getModelByResellerId(String resellerId, Page<ResellerProductModel> requestPage) {

        MPJLambdaWrapper<ResellerProduct> wrapper = new MPJLambdaWrapper<>();
        IPage<ResellerProductModel> resellerProductModelIPage = baseMapper.selectJoinPage(requestPage, ResellerProductModel.class, wrapper
                .selectAll(ResellerProduct.class)
                .select(Product::getMaterialDesc, Product::getBasePrice)
                .innerJoin(Product.class, Product::getId, ResellerProduct::getProductId)
                .eq(Product::getDelFlag, CommonConstant.DEL_FLAG_0)
                .eq(ResellerProduct::getResellerId, resellerId));

        Set<String> productIds = resellerProductModelIPage.getRecords().stream().map(ResellerProduct::getProductId).collect(Collectors.toSet());
        Map<String, PriceComputeResult> compute = cmsManager.getProductPriceManager().compute(resellerId, productService.getByIds(productIds));

        resellerProductModelIPage.getRecords().forEach(resellerProductModel -> {
            resellerProductModel.setCalculatePrice(compute.get(resellerProductModel.getProductNumber()).getPrice());
        });

        return resellerProductModelIPage;
    }
}
